/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Filter;
import com.hboxs.asl.Order;
import com.hboxs.asl.dao.AdDao;
import com.hboxs.asl.dao.AdPositionDao;
import com.hboxs.asl.entity.Ad;
import com.hboxs.asl.entity.AdPosition;
import com.hboxs.asl.service.AdService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Service - 广告
 */
@Service("adServiceImpl")
public class AdServiceImpl extends BaseServiceImpl<Ad, Long> implements AdService {

    @Resource(name = "adDaoImpl")
    public void setBaseDao(AdDao adDao) {
        super.setBaseDao(adDao);
    }

    @Resource(name = "adPositionDaoImpl")
    private AdPositionDao adPositionDao;

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public void save(Ad ad) {
        super.save(ad);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public Ad update(Ad ad) {
        return super.update(ad);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public Ad update(Ad ad, String... ignoreProperties) {
        return super.update(ad, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public void delete(Long... ids) {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = "adPosition", allEntries = true)
    public void delete(Ad ad) {
        super.delete(ad);
    }

    @Override
    public List<Ad> findListByAdPosition(String adPositionName) {
        List<Filter> adp_fs = new ArrayList<>();
        adp_fs.add(Filter.eq("name", adPositionName));
        List<Order> orders = new ArrayList<>();
        List<AdPosition> adPositions = adPositionDao.findList(null, 1, adp_fs, null);
        List<Filter> ad_fs = new ArrayList<>();
        ad_fs.add(Filter.eq("adPosition", adPositions.get(0)));
        orders.add(Order.asc("order"));
        return super.findList(null, ad_fs, orders);
    }
}